package com.chen.nongansampling.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import com.chen.nongansampling.R;
import com.chen.nongansampling.bean.SampledListBean;
import com.chen.nongansampling.fragment.LabelPrintedFragment;
import com.chen.nongansampling.fragment.SampledFragment;
import com.chen.nongansampling.utils.StringUtils;
import com.chen.nongansampling.viewModel.SamplingViewModel;

import java.util.List;
import static com.chen.nongansampling.MainActivity.yx;
public class SampledRecycleViewAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder>
{
        private List<SampledListBean> list;

    public Activity getView() {
        return view;
    }


    private Activity view;
    private SampledFragment sf;


    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    private RecyclerView recyclerView;

//        public NewsAdapter(List<News> list) {
//            this.list = list;
//        }

    public SampledRecycleViewAdapter() {

    }
       public SampledRecycleViewAdapter(Activity view,List<SampledListBean> list,SampledFragment sf) {
//           if (list.size()!=0)
//           this.list.clear();
            this.view = view;
           this.sf = sf;
            this.list=list;
       }
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = null;
            v = view.getLayoutInflater().inflate(R.layout.sampled_recycleview_item, parent, false);
            RecyclerView.ViewHolder holder = null;
            holder = new SampledViewHolder(v);

            return holder;
        }

    ListviewEditTextAdapter listviewEditTextAdapter;
    @Override
    public void  onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                    //样品时间
                   ((SampledViewHolder) holder).sampleNumber.setText(StringUtils.changeString(list.get(position).getSampleNumber()));
//            ((SampledViewHolder) holder).samplingTime.setText(list.get(position).getSamplingTime().split("T")[0]);
            //摊位号
            ((SampledViewHolder) holder).boothNumber.setText(StringUtils.zifusuoduan(list.get(position).getBoothNumber()));
            //经销户
            ((SampledViewHolder) holder).operatingHouseholds.setText(list.get(position).getOperatingHouseholds());
//            ((SampledViewHolder) holder).result.setText(list.get(position).getResult());
        //样品名称
        ((SampledViewHolder) holder).sampleName.setText(StringUtils.yangpin(list.get(position).getSampleName()));

        // ((SampledViewHolder) holder).sampleName.setText(list.get(position).getSampleName());
        //市场名称
        ((SampledViewHolder) holder).marketName.setText(StringUtils.yangpin(list.get(position).getMarketName()));
        //检测项目
        ((SampledViewHolder) holder).type.setText(StringUtils.yangpin(list.get(position).getType()));
        //实现点击效果
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SamplingViewModel model = ViewModelProviders.of(sf.getActivity()).get(SamplingViewModel.class);
                if (model!=null)
                {
                    model.setSamplingPkId(list.get(position).getPkId());
                }
                yx=false;
                sf.getActivity().getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, LabelPrintedFragment.newInstance())
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                        .addToBackStack(null)
                        .commit();
            }
        });
//
//sampleName
//
//       //     ((SampledViewHolder) holder).marketName(list.get(position).getMarketName());
//
//
//            ((SampledViewHolder) holder).marketName.setText(list.get(position).getMarketName());
//
//            ((SampledViewHolder) holder).sampleNumber.setText(list.get(position).getSampleNumber());
//       //     sampleNumber
    }

//        @Override
//        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//            ((SampledViewHolder) holder).sampleName.setText(list.get(position).getSampleName());
////            ((SampledViewHolder) holder).sampleNumber.setText(StringUtil.changeString(list.get(position).getSampleNumber()));
////
//
// ((SampledViewHolder) holder).samplingTime.setText(list.get(position).getSamplingTime().split("T")[0]);
//            ((SampledViewHolder) holder).boohNumber.setText(list.get(position).getBoothNumber());
//            ((SampledViewHolder) holder).operatingHouseholds.setText(list.get(position).getOperatingHouseholds());
////            ((SampledViewHolder) holder).result.setText(list.get(position).getResult());
//            ((SampledViewHolder) holder).type.setText(list.get(position).getType());
//
//
//
//       //     ((SampledViewHolder) holder).marketName(list.get(position).getMarketName());
//
//
//            ((SampledViewHolder) holder).marketName.setText(list.get(position).getMarketName());
//
//            ((SampledViewHolder) holder).sampleNumber.setText(list.get(position).getSampleNumber());
//       //     sampleNumber
//        }

        @Override
        public int getItemCount() {
            return list.size();
        }



}

class SampledViewHolder extends RecyclerView.ViewHolder {
    public TextView sampleName, marketName, boothNumber,sampleNumber,operatingHouseholds,type;

    public SampledViewHolder(View itemView) {
        super(itemView);
        sampleName = itemView.findViewById(R.id.sampleName);
        boothNumber=itemView.findViewById(R.id.boothNumber);
        operatingHouseholds=itemView.findViewById(R.id.operatingHouseholds);
        sampleNumber=itemView.findViewById(R.id.sampleNumber);

        type=itemView.findViewById(R.id.type);

        marketName=itemView.findViewById(R.id.marketName);

    }

}



